package com.xysq.activity;

import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;

import com.rockcent.action.CallbackListener;
import com.rockcent.model.BClientCustomerBO;
import com.rockcent.model.CustomerCouponBO;
import com.rockcent.model.constant.RealNameAuthType;
import com.umeng.analytics.MobclickAgent;
import com.xysq.fragment.TipsDetailsFragment;
import com.xysq.fragment.EvaluateFragment;
import com.xysq.lemon.R;
import com.xysq.util.ToastUtil;
import com.xysq.util.UserInfoKeeper;

import java.util.ArrayList;
import java.util.List;

public class TipsActivity extends BaseActivity implements EvaluateFragment.OnFragmentInteractionListener, TipsDetailsFragment.OnFragmentInteractionListener, View.OnClickListener {
    private List<Fragment> fragmentList;
    private Button buyNowBtn;
    private int couponModelId;
    FragmentPagerAdapter adapter;
    ViewPager pager;
    public static final String COUPONMODEL_ID = "couponModel_Id";
    public static final String CUSTOMER_ID = "customer_id";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_tips);
        initView();
        processFragmentList();
    }

    private void processFragmentList() {
        fragmentList = new ArrayList<Fragment>();
        fragmentList.add(TipsDetailsFragment.newInstance(couponModelId));
        adapter = new GoogleMusicAdapter(getSupportFragmentManager());

        pager = (ViewPager) findViewById(R.id.pager);
        pager.setAdapter(adapter);
    }

    private void initView() {
        couponModelId = getIntent().getIntExtra(COUPONMODEL_ID, 0);
        buyNowBtn = (Button) findViewById(R.id.btn_buy_now);
        buyNowBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                MobclickAgent.onEvent(TipsActivity.this, "1008");
                if (TextUtils.isEmpty(UserInfoKeeper.readPhoneNumber(TipsActivity.this))) {
                    startActivity(new Intent(TipsActivity.this, LoginActivity.class));
                } else {
                    checkIdentify();
                }
            }
        });
    }

    //获取用户身份认证状态
    private void checkIdentify() {
        appAction.getBClientCustomer(UserInfoKeeper.readPhoneNumber(this), new CallbackListener<BClientCustomerBO>() {
            @Override
            public void onSuccess(BClientCustomerBO data) {
                if (data.getIsRealnameAuth() == RealNameAuthType.UNAUTH) {//未实名认证，跳到实名认证页面
                    startActivity(new Intent(TipsActivity.this, IdentifyActivity.class));
                } else if (data.getIsRealnameAuth() == RealNameAuthType.FAIL) {
                    Intent intent = new Intent(TipsActivity.this, IdentifyActivity.class);
                    intent.putExtra(IdentifyActivity.FAIL, IdentifyActivity.FAIL);
                    startActivity(intent);
                } else if (data.getIsRealnameAuth() == RealNameAuthType.AUTHING) {
                    ToastUtil.showShort(TipsActivity.this, getResources().getString(R.string.toast_authing));
                } else if (data.getIsRealnameAuth() == RealNameAuthType.AUTH) {
                    getCustomerCouponType();
                }
            }

            @Override
            public void onFailure(String errorCode, String message) {
                ToastUtil.showShort(TipsActivity.this, "未实名认证");
            }
        });
    }

    private void getCustomerCouponType() {
        appAction.viewCustomerCoupon(couponModelId, new CallbackListener<CustomerCouponBO>() {
            @Override
            public void onSuccess(CustomerCouponBO data) {
                if (!String.valueOf(data.getCustomerId()).equals(UserInfoKeeper.readKcouponId(TipsActivity.this))) {
                    String s = UserInfoKeeper.readKcouponId(TipsActivity.this);
//                    Intent intent = new Intent(TipsActivity.this, PayActivity.class);
                    Intent intent = new Intent(TipsActivity.this, CustomerPayActivity.class);
                    intent.putExtra(CustomerPayActivity.COUPON_MODEL_ID, Integer.valueOf(couponModelId));
//                    intent.putExtra(PayActivity.COUPON_CATEGORY, CustomerCouponCategory.CLIENT);
//                    intent.putExtra(CustomerPayActivity.COUPON_MODEL_ID, Integer.valueOf(couponModelId));
                    startActivity(intent);
                } else {
                    ToastUtil.showShort(TipsActivity.this, getResources().getString(R.string.toast_can_no_rewart_own_kcupon));
                }
            }

            @Override
            public void onFailure(String errorCode, String message) {

            }
        });
    }

    @Override
    public void onFragmentInteraction(Uri uri) {

    }

    @Override
    public void onClick(View view) {
    }

    class GoogleMusicAdapter extends FragmentPagerAdapter {
        public GoogleMusicAdapter(FragmentManager fm) {
            super(fm);
        }


        @Override
        public Fragment getItem(int position) {
            return fragmentList.get(position);
        }

        @Override
        public CharSequence getPageTitle(int position) {
            return null;
        }

        @Override
        public int getCount() {
            return fragmentList.size();
        }
    }

    @Override
    protected void onResume() {
        super.onResume();
        MobclickAgent.onPageStart("技能页面");
        MobclickAgent.onResume(this);
    }

    @Override
    protected void onPause() {
        super.onPause();
        MobclickAgent.onPageEnd("技能页面");
        MobclickAgent.onPause(this);
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        if (requestCode == UserActivity.IDENTIFY_CODE) {
            boolean result = data.getBooleanExtra(UserActivity.RESULT, true);
            if (result) {//用户保存信息后返回
                startActivity(new Intent(TipsActivity.this, IdentifyActivity.class));
            } else {//用户直接返回，不做任何操作
            }
        }
        super.onActivityResult(requestCode, resultCode, data);
    }
}
